__author__="Peter"
__date__ ="$Apr 24, 2011 3:21:56 PM$"

from graphics import *
from math import *
from lib.core_classes import *
from lib.applied_math import *



def main():
    win = GraphWin("Project Awesome",800,630)
    menuInitialization(win)

    mainGameField = GameField(win, Point(200, 330), Point(600, 330))
    mainGameField.draw()
    start = False
    chargeToPlace = None
    while (not start):
        mouse = win.getMouse()
        x = mouse.getX()
        y = mouse.getY()
        if y<=30:
            # Clicked on "Place Charge"
            if (290<=x<=420):
                if chargeToPlace != None:
                    mainGameField.addCharge(chargeToPlace)
                chargeToPlace = Charge(Point(0,0), 1)
            # Clicked on "Positive Charge"
            elif (200<=x<=230) and (chargeToPlace != None):
                chargeToPlace.setCharge(1)
            # Clicked on "Negative Charge"
            elif (230<=x<=260) and (chargeToPlace != None):
                chargeToPlace.setCharge(-1)
            # Clicked on "Start"
            elif (420<=x<=800)and (chargeToPlace != None):
                mainGameField.addCharge(chargeToPlace)
                start = True
        elif chargeToPlace != None:
            mainGameField.placeCharge(chargeToPlace, mouse)


    finished = False
    while not finished:
        finished = mainGameField.update()
        if finished:
            message = Text(Point(400,315),"LEVEL COMPLETE");
            message.draw(win)
            time.sleep(2.5)
        time.sleep(SEC_PER_UPDATE)

def posNeg(x,y,num, win):
    circ=Circle(Point(x,y),8)
    circ.setOutline('white')
    lin1=Line(Point(x+6,y),Point(x-6,y))
    lin1.setOutline('white')
    lin2=Line(Point(x,y-6),Point(x,y+6))
    lin2.setOutline('white')
    if num==1:
        return circ.draw(win), lin1.draw(win), lin2.draw(win)
    else:
        return lin1.draw(win), circ.draw(win)

def menuInitialization(win):
    menuBox = Rectangle(Point(0,0),Point(800,30))
    menuBox.draw(win)

    newChargeButton = Rectangle(Point(0,30),Point(200,0))
    newChargeButton.setFill('yellow')
    newChargeButtonlabel=Text(Point(100,15),"Type of Charge:")
    newChargeButton.draw(win)
    newChargeButtonlabel.draw(win)

    positiveChargeButton = Rectangle(Point(200,30),Point(230,0))
    positiveChargeButton.setFill('blue')
    negativeChargeButton = Rectangle(Point(230,30),Point(260,0))
    negativeChargeButton.setFill('red')
    negativeChargeButton.draw(win)
    positiveChargeButton.draw(win)
    posNeg(215,15,1, win)
    posNeg(245,15,-1, win)

    placeChargeButton = Rectangle(Point(290,30),Point(420,0))
    placeChargeButton.setFill('Purple')
    placeChargeButtonlabel=Text(Point(355,15),"Place Charge")
    placeChargeButton.draw(win)
    placeChargeButtonlabel.draw(win)
    
    startButton= Rectangle(Point(420,30),Point(800,0))
    startButton.setFill('green')
    startButtonlabel=Text(Point(610,15),"Start")
    startButton.draw(win)
    startButtonlabel.draw(win)


main()